package com.fahai.utils;

import com.fahai.web.entity.DataAnalyse;
import com.fahai.web.service.DataAnalysisService;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Properties;


public class ExcelExportForDataAnalyse {
	private static Logger logger = Logger.getLogger(ExcelExportForDataAnalyse.class);
	@Autowired
	DataAnalysisService service;
	/*properties*/
	private Properties properties;
	public ExcelExportForDataAnalyse(String propFilePath){
		try {
			properties = PropertiesLoaderUtils.loadAllProperties(propFilePath);
		} catch (IOException e) {
			logger.error(e);
			throw new RuntimeErrorException(new Error(e.getMessage()));
		}
	}	

	/*取配置文件excelToBean.properties的值**/
	public Object getVale(String key) {
		return properties.getProperty(key);
	}
	

	/**
	 *导出xls
	 * @param request
	 * @param resp
	 * @param vos 查询结果
	 * @return
	 * @throws IOException
	 */
	public static void expExcel(HttpServletRequest request, HttpServletResponse resp, List<DataAnalyse> vos) throws IOException {

		HSSFWorkbook wb = new HSSFWorkbook();
		request.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/x-download");
		String fileName = "数据分析"+System.currentTimeMillis()+".xls";
		fileName = URLEncoder.encode(fileName, "UTF-8");
		resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
		String[] headers={"用户Id","用户姓名","用户电话","用户注册时间","用户归属地","用户余额","律师Id","律师姓名","律师电话","律师余额","律师服务地","律师来源","雇佣类型","律师状态","咨询发起时间","首次咨询时间","通话时长","通话结果","咨询有效性","发起方","咨询发起位置"};
		HSSFSheet sheet = wb.createSheet("数据分析");
		sheet.setDefaultRowHeight((short) (2 * 256));
		//sheet.setColumnWidth(0, 50 * 160);
		HSSFCellStyle cellStyle = wb.createCellStyle();

		HSSFRow row =sheet.createRow(0);
		HSSFFont headerFont = wb.createFont(); // 创建字体样式
		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
		headerFont.setFontName("宋体"); // 设置字体类型
		//headerFont.setColor(HSSFFont.COLOR_RED);
		headerFont.setFontHeightInPoints((short) 14); // 设置字体大小
		cellStyle.setFont(headerFont); // 为标题样式设置字体样式
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
		for (int i=0;i<headers.length;i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(headers[i]);
			cell.setCellStyle(cellStyle);
		}

		HSSFFont font = wb.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 14);

		for (int i = 0; i < vos.size(); i++)
		{
			HSSFRow row1 = sheet.createRow((int) i + 1);
			//row1.setRowStyle(cellStyle);
			DataAnalyse vo = vos.get(i);
			int j=0;
			//row1.createCell(j++).setCellValue(i + 1);
			row1.createCell(j++).setCellValue(String.valueOf(vo.getUserid()==null?"":vo.getUserid()));//客户ID
			row1.createCell(j++).setCellValue(String.valueOf(vo.getUname()==null?"":vo.getUname()));//客户姓名/昵称
			row1.createCell(j++).setCellValue(String.valueOf(vo.getUphone()==null?"":vo.getUphone()));//用户手机号
			row1.createCell(j++).setCellValue(String.valueOf(vo.getCreateTime()==null?"":DateUtil.TimeStampToString(vo.getCreateTime(), "yyyy-MM-dd HH:mm:ss") ));//用户注册时间
			row1.createCell(j++).setCellValue(String.valueOf(vo.getUphoneArea()==null?"":vo.getUphoneArea()));//用户归属地
			row1.createCell(j++).setCellValue(String.valueOf(vo.getUrmoney()==null?"":vo.getUrmoney()));//用户余额
			row1.createCell(j++).setCellValue(String.valueOf(vo.getAlawyerid()==null?"":vo.getAlawyerid() ));//服务律师ID
			row1.createCell(j++).setCellValue(String.valueOf(vo.getAlawyername()==null?"":vo.getAlawyername() ));//服务律师名称
			row1.createCell(j++).setCellValue(String.valueOf(vo.getAlawyerPhone()==null?"":vo.getAlawyerPhone() ));//服务律师电话
			row1.createCell(j++).setCellValue(String.valueOf(vo.getAllDistillMoney()==null?"":vo.getAllDistillMoney()));//服务律师余额
			row1.createCell(j++).setCellValue(String.valueOf(vo.getAcname() ==null?"":vo.getAcname()));//服务地区
			row1.createCell(j++).setCellValue("");//TODO 律师来源
			row1.createCell(j++).setCellValue("");//TODO 雇佣类型
			row1.createCell(j++).setCellValue(String.valueOf(vo.getLawyerStatesStr() ==null?"":vo.getLawyerStatesStr()));//律师状态
			row1.createCell(j++).setCellValue(String.valueOf(vo.getStartTimeStr()==null?"":vo.getStartTimeStr()) );//开始时间

			row1.createCell(j++).setCellValue(String.valueOf(vo.getFirstTime()==null?"":DateUtil.TimeStampToString(vo.getFirstTime(), "yyyy-MM-dd HH:mm:ss") ));//第一次咨询时间

			row1.createCell(j++).setCellValue(String.valueOf(vo.getCalledDuration()==null?"":vo.getCalledDuration()));//通话时长
			row1.createCell(j++).setCellValue(CommonUtil.parseThjg(vo.getCallStatus()==null?0:vo.getCallStatus(),vo.getRejectStatus()==null?0:vo.getRejectStatus()));//通话结果
			row1.createCell(j++).setCellValue(String.valueOf(vo.getIsEffectiveStr()==null?"":vo.getIsEffectiveStr()) );//是否有效
			row1.createCell(j++).setCellValue(String.valueOf(vo.getInitiatorStr()==null?"":vo.getInitiatorStr() ));//发起方

			row1.createCell(j++).setCellValue(String.valueOf(vo.getProid() ==null?"":vo.getProid()==1?"法海问法":"手机M站"));//TODO 咨询发起位置



		}

		OutputStream out = resp.getOutputStream();
		wb.write(out);
		out.close();
	}




}
